package Tool_class;

import java.util.Calendar;

//判断日期并进行日期转换，没有超过20个小时的通话
public class Judge_date {
    private Calendar start_time;
    private Calendar finish_time;
    public Judge_date(Calendar s,Calendar f){
        start_time=s;
        finish_time=f;
    }
    //判断当天是否是周日
    public boolean Is_Sunday(Calendar date){
        int ss= date.get(Calendar.DAY_OF_WEEK); //秒
        if(ss==1) return true;
        return false;//Calendar中1-星期天,2-星期一,3-星期二,4-星期三,5-...
    }

    //得到当天是本周的第几天
    public int getWeekDay(int year, int month, int day)
    {
        int weekday;
        for(;month<=0||month>12;)
        {
            if(month<=0)
            {
                year-=1;
                month+=12;
            }
            else if(month>12)
            {
                year+=1;
                month-=12;
            }
        }
        weekday=year>0?(5+(year+1)+(year-1)/4-(year-1)/100+(year-1)/400)%7: (5+year+year/4-year/100+year/400)%7;
        weekday=month>2?(weekday+2*(month+1)+3*(month+1)/5)%7: (weekday+2*(month+2)+3*(month+2)/5)%7;
        if(((year%4==0&&year%100!=0)||year%400==0)&&month>2)
        {
            weekday=(weekday+1)%7;
        }
        weekday=(weekday+day)%7;
        return weekday;
    }


    //计算通话时长
    public int judge(){
        int sy,smonth,sd,sh,sm,ss;
        @SuppressWarnings("unused")
        int ey,emonth,ed,eh,em,es;
        float totalsecond;
        int totalminiute;

        sy=start_time.get(Calendar.YEAR);//年
        smonth=start_time.get(Calendar.MONTH) + 1;//月
        sd=start_time.get(Calendar.DATE);       //日
        sh=start_time.get(Calendar.HOUR_OF_DAY);
        sm=start_time.get(Calendar.MINUTE);
        ss=start_time.get(Calendar.SECOND);

        ey=finish_time.get(Calendar.YEAR);//年
        emonth=finish_time.get(Calendar.MONTH) + 1;//月
        ed=finish_time.get(Calendar.DATE);       //日
        eh=finish_time.get(Calendar.HOUR_OF_DAY);
        em=finish_time.get(Calendar.MINUTE);
        es=finish_time.get(Calendar.SECOND);
        /*
         *计算三月末四月初的周日
         *
         */
        int i;
        for(i = 0; i < 10; i++){
            if(getWeekDay(sy,3,26+i)==0){//是周日//开始年，3月，0/26，1/27，2/28，3/29，4/30，5/31，6/32/1，7/33/2，8/34/3，9/35/4
                if(i == 0 || i == 1)continue;//周日 i=0、1
                else break;//周日，i=2,3,4,5,6,7,8,9
            }//i
            /*
             *计算十月最后一个周日
             *
             */
        }
        int j;
        for(j = 7; j > 0; j --){
            if(getWeekDay(sy,10,31-j)==0)//10月24,25,26,27,28,29,30日
                break;//周日，j=7,6,5,4,3,2,1
        }

        if(ed > sd)//计算两个日期实际相差时长
            totalsecond = 24*60*60-(sh*60*60+sm*60+ss)+eh*60*60+em*60+es;
        else totalsecond = (eh*60*60+em*60+es)-(sh*60*60+sm*60+ss);

        float haha=totalsecond%60;//秒数取余，
        if(haha>0.0)
            totalminiute = (int) (totalsecond/60)+1;
        else totalminiute = (int) (totalsecond/60);

        //通话时长大于20小时
        int a=(int) (totalminiute/60);//计算通话时长，向下取整，一个有多少小时
        if(a>20){//通话时长大于20小时，返回0，因为计算出来的分钟不会出现0，“不满1分钟按1min计算”原则
            return 0;
        }

        //周日，i=2,3,4,5,6,7,8,9
        if(smonth == 3 && sd == 26 + i && sh < 2 && eh >= 3)
            totalminiute -= 60;//三月末、四月初的周日
        //周日，j=7,6,5,4,3,2,1
        if(smonth == 10 && sd == 31 - j && sh < 3)
            totalminiute += 60;//十月份的最后一个周日
        System.out.println(totalminiute);   //按照电话账单最终计算出来的通话时长
        return totalminiute;
    }
}
